package com.lxh.empwork.web.servlet;

import com.github.pagehelper.util.StringUtil;
import com.lxh.empwork.common.Const;
import com.lxh.empwork.entity.Emp;
import com.lxh.empwork.services.EmpServices;
import com.lxh.empwork.services.impl.EmpServicesImpl;
import com.lxh.empwork.util.DateUtils;
import com.lxh.empwork.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;


@WebServlet("/emp")
public class EmpController extends BaseServlet {
    private EmpServices es = new EmpServicesImpl();

    public String getEmpByEno(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        int eno = StringUtils.stringToInt(request.getParameter("eno"));
        Emp e = es.getEmpByEno(eno);
        request.setAttribute("e", e);
        return Const.FORWARD+"/update.jsp";
    }

    public String updateEmp(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        //获取用户传递的擦拿书
        Map<String, String[]> parameterMap = request.getParameterMap();
        Emp e = new Emp();
        //封装用户提交参数到数据库中
        BeanUtils.populate(e, parameterMap);
        //因为不想想转换器所以手动封装日期
        e.setBornDate(DateUtils.stringToDate(request.getParameter("born_date")));
        //业务
        boolean ans = es.updateEmp(e);
        String msg = "修改失败";
        if (ans) {
            msg = "修改成功";
        }
        request.setAttribute("msg", msg);
        return Const.FORWARD + "/message.jsp";
    }

    public String addEmp(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        //获取用户传递的擦拿书
        Map<String, String[]> parameterMap = request.getParameterMap();
        Emp e = new Emp();
        //封装用户提交参数到数据库中
        BeanUtils.populate(e, parameterMap);
        //因为不想想转换器所以手动封装日期
        e.setBornDate(DateUtils.stringToDate(request.getParameter("born_date")));
        //业务
        boolean ans = es.addEmp(e);
        String msg = "新增失败";
        if (ans) {
            msg = "新增成功";
        }
        request.setAttribute("msg", msg);
        return Const.FORWARD + "/message.jsp";
    }

    public String del(HttpServletRequest request, HttpServletResponse response) {
        int eno = StringUtils.stringToInt(request.getParameter("eno"));
        boolean ans = es.delEmp(eno);
        if (ans) {
            return Const.SUCCESS;
        } else {
            return Const.FAIL;
        }
    }

    public String getEmpsByMaps(HttpServletRequest request, HttpServletResponse response) {
        // 获取用户数据
        String ename = request.getParameter("ename");
        String start = request.getParameter("start");
        String end = request.getParameter("end");
        String esex = request.getParameter("esex");
        int pageNum = StringUtils.stringToInt(request.getParameter("pageNum"));
        //封装为map
        Map<String, String> maps = new HashMap<>();
        maps.put("ename", ename);
        maps.put("start", start);
        maps.put("end", end);
        maps.put("esex", esex);
        PageInfo<Emp> el = es.getEmpsByMapsWithPage(maps, pageNum);
        request.setAttribute("info", el);
        return Const.FORWARD + "/index.jsp";
    }




    public String doLogin(HttpServletRequest request, HttpServletResponse response) {
        String ename = request.getParameter("ename");
        String pwd = request.getParameter("pwd");
        //访问业务
        Emp emp = es.doLogin(ename, pwd);
        //判断
        if (emp != null) {
            System.out.println(1);
            request.getSession().setAttribute(Const.IS_LOGIN, emp);
            // 动态分页地址
            return Const.REDIRECT + "/emp?method=getEmpsByMaps";
        } else {
            System.out.println(2);
            return Const.REDIRECT + "/login.jsp";
        }
    }

}
